/*
 * Copyright 2016 drakeet. https://github.com/drakeet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epfresh.adapter;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.utils.T;
import com.epfresh.bean.*;
import com.epfresh.bean.HomeGoodsOften;
import com.epfresh.utils.FormatUtil;
import com.epfresh.views.tag.TagCloudView;

import java.io.IOException;
import java.util.ArrayList;

import multitype.ItemViewBinder;
import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

public class ViewBinderOfferMy
        extends ItemViewBinder<OfferMyList, ViewBinderOfferMy.ViewHolder> implements View.OnClickListener {//},RadioGroup.OnCheckedChangeListener {

    OnCartAddListener onCartAddListener;
    OfferMyList bean;
    //红 黑 灰 绿  浅绿 浅灰
    int colors[] = {Color.RED, Color.parseColor("#434343"), Color.parseColor("#999999"), Color.parseColor("#24af75"), Color.parseColor("#f7fff6"), Color.parseColor("#f8f8f8")};//价格激活红色，失活黑色横线

    public OnCartAddListener getOnCartAddListener() {
        return onCartAddListener;
    }

    public void setOnCartAddListener(OnCartAddListener onCartAddListener) {
        this.onCartAddListener = onCartAddListener;
    }

    FreshItemListener freshItemListener;

    public interface FreshItemListener {
        void freshItem(int type, int position, OfferMyList goods, View v);
    }

    public FreshItemListener getFreshItemListener() {
        return freshItemListener;
    }

    public void setFreshItemListener(FreshItemListener freshItemListener) {
        this.freshItemListener = freshItemListener;
    }

    public interface OnCartAddListener {
        void onCartAdd(int type, int position, OfferMyList goods, View v);
    }

    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        if (o != null && o instanceof Integer) {
            int position = (int) o;
            if (v.getTag(R.id.item_key_status) instanceof OfferMyList)
                bean = (OfferMyList) v.getTag(R.id.item_key_status);
            else
                return;
            if (v.getId() == R.id.ed_price) {//购物车手动输入
                onCartAddListener.onCartAdd(1, position, bean, v);
            } else if (v.getId() == R.id.iv_add_count) {//添加
                if (bean.isAcceptOrders())
                    onCartAddListener.onCartAdd(2, position, bean, v);
                else
                    T.toast("暂不接单");
            } else if (v.getId() == R.id.iv_remove_count) {//减少
                onCartAddListener.onCartAdd(3, position, bean, v);

            } else if (v.getId() == R.id.tv_product_title || v.getId() == R.id.iv_product || v.getId() == R.id.order_ll) {//
                onCartAddListener.onCartAdd(5, position, bean, v);

            } else if (v.getId() == R.id.v_cart_bg) {

            } else if (v.getId() == R.id.iv_often_delete) {
                onCartAddListener.onCartAdd(0, position, bean, v);
            } else if (v.getId() == R.id.tv_home_often_compare) {
                onCartAddListener.onCartAdd(7, position, bean, v);
            } else if (v.getId() == R.id.ll_offer) {
                if (!bean.isAcceptOrders()) {
                    T.toast("暂不接单");
                    return;
                }
                if (bean.getStatus() == 3 || bean.getStatus() == 4 || bean.getStatus() == 5)
                    onCartAddListener.onCartAdd(10, position, bean, v);//再次问价
            } else if (v.getId() == R.id.gif_1 || v.getId() == R.id.gif_2 || v.getId() == R.id.gif_3 || v.getId() == R.id.gif_4) {
                if (!bean.isAcceptOrders()) {
                    T.toast("暂不接单");
                    return;
                }
                if(bean!=null&&(bean.getStatus()==1||bean.getStatus()==2))
                    T.toast("要货" + bean.getQuantity() + bean.getChargeUnit());
            }

        }
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_offer_my, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder vh, @NonNull OfferMyList bean) {
        initState(vh, bean);
        ImageLoader.getInstance().displayImage(bean.getImageUrl(), vh.iv);
        vh.tvTitle.setText(bean.getTitle());
        vh.tv_home_often_store.setText(bean.getName());
        vh.tv_price_old.setText("" + bean.getOriginalPrice());
        vh.tvUnitPackage.setText(bean.getPackingSpecification());
        vh.tv_offer_price.setText(bean.getNowPrice());

        vh.tv_create_time.setText(bean.getCreateTime().substring(0, 16));
        vh.tv_number_bottom.setText("要货" + bean.getQuantity() + bean.getChargeUnit());
        String goodsCount = "";
        if (bean.getShoppingCarProductCount() != null) {
            goodsCount = "" + bean.getShoppingCarProductCount();
        }
        goodsCount = FormatUtil.subZeroAndDot(goodsCount + "");
        vh.tvCountAdd.setText(goodsCount);
        vh.countAddUnderlineTv.setText(goodsCount);
        vh.tvChargeNumber.setText("已选" + goodsCount + bean.getChargeUnit());

        vh.ll_offer.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.ll_offer.setTag(R.id.item_key_status, bean);
        vh.ll_offer.setOnClickListener(this);
        vh.vCartBg.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.vCartBg.setTag(R.id.item_key_status, bean);
        vh.vCartBg.setOnClickListener(this);
        vh.vContent.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.vContent.setTag(R.id.item_key_status, bean);
        vh.vContent.setOnClickListener(this);
        vh.tvTitle.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.tvTitle.setTag(R.id.item_key_status, bean);
        vh.tvTitle.setOnClickListener(this);

        vh.tv_home_often_compare.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.tv_home_often_compare.setTag(R.id.item_key_status, bean);
        vh.tv_home_often_compare.setOnClickListener(this);

        vh.tvCountAdd.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.tvCountAdd.setTag(R.id.item_key_status, bean);
        vh.ivCountAdd.setTag(R.id.item_key_status, bean);
        vh.ivCountAdd.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.ivCountRemove.setTag(R.id.item_key_position, vh.getAdapterPosition());
        vh.ivCountRemove.setTag(R.id.item_key_status, bean);
        vh.ivCountRemove.setTag(R.id.item_key_position, vh.getAdapterPosition());

        vh.tvCountAdd.setOnClickListener(this);
        vh.ivCountAdd.setOnClickListener(this);
        vh.ivCountRemove.setOnClickListener(this);
        if (bean.isAcceptOrders()&&bean.getProductStatus() == 1){
            vh.ivCountAdd.setImageResource(R.mipmap.price_add);
            vh.ll_offer.setVisibility(View.VISIBLE);

        } else {
            vh.ivCountAdd.setImageResource(R.mipmap.price_grey);
            vh.ll_offer.setVisibility(View.GONE);
            vh.tv_offer_time_remind.setText("");
            vh.vFlag.setImageResource(R.mipmap.offer_up_grey);
            vh.rl_msg.setBackgroundColor(colors[5]);
            vh.tv_create_time.setTextColor(colors[2]);
            vh.tv_number_bottom.setTextColor(colors[2]);
        }
        if (bean.getProductStatus() != 1) {
            vh.tvTitle.setTextColor(Color.parseColor("#959595"));
            vh.tv_home_often_store.setTextColor(Color.parseColor("#959595"));
            vh.ll_home_often_no.setVisibility(View.VISIBLE);
            vh.ll_package.setVisibility(View.GONE);
        } else {
            vh.tvTitle.setTextColor(Color.parseColor("#313131"));
            vh.tv_home_often_store.setTextColor(Color.parseColor("#434343"));
            vh.ll_home_often_no.setVisibility(View.GONE);
            vh.ll_package.setVisibility(View.VISIBLE);
        }
        showCartAddView(vh, bean);
    }

    void initState(@NonNull ViewHolder vh, @NonNull OfferMyList bean) {
        vh.gif_1.setVisibility(View.GONE);
        vh.gif_2.setVisibility(View.GONE);
        vh.gif_3.setVisibility(View.GONE);
        vh.gif_4.setVisibility(View.GONE);
        vh.tv_offer_time_remind.setVisibility(View.GONE);

        switch (bean.getStatus()) {

            //1报价中 2已报价 3放弃报价 4放弃问价(没有？) 5报价失效
            case 1://绿色动图，灰色倒计时 努力报价中
                vh.tv_offer_time_remind.setVisibility(View.VISIBLE);

                vh.gif_1.setVisibility(View.VISIBLE);
                GifDrawable gifDrawable = (GifDrawable) vh.gif_1.getDrawable();
                gifDrawable.start();
                vh.tv_price_old.setTextColor(colors[0]);
                vh.tv_price_old.setPaintFlags(vh.tv_price_old.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                vh.tv_offer_price.setVisibility(View.GONE);
                vh.tv_offer_hint.setVisibility(View.VISIBLE);
                vh.tv_offer_hint.setTextColor(colors[2]);
                vh.tv_offer_hint.setText(bean.getOfferStatusCopy());
                String buyTime = bean.getOfferBuyRemindTime();
                vh.tv_offer_time_remind.setText(bean.getOfferBuyRemindTime());
                if (buyTime.endsWith("5:00") || buyTime.endsWith("0:00")) {
                    freshItemListener.freshItem(0, 0, bean, null);
                }
                if ("00:00".endsWith(buyTime)) {
                    bean.setStatus(3);// TODO: 2018/7/9
                    bean.setOfferStatusCopy("忙碌，过会问");
                }
                vh.vFlag.setImageResource(R.mipmap.offer_up_green);
                vh.rl_msg.setBackgroundColor(colors[4]);
                vh.tv_create_time.setTextColor(colors[1]);
                vh.tv_number_bottom.setTextColor(colors[1]);
                break;
            case 2://5分钟内部红动图，红色倒计时；充裕红圈动图，灰色倒计时 新价格 老价格变黑横线
                vh.tv_offer_time_remind.setVisibility(View.VISIBLE);

                vh.tv_price_old.setTextColor(colors[1]);
                vh.tv_price_old.setPaintFlags(vh.tv_offer_price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                vh.tv_offer_price.setVisibility(View.VISIBLE);
                vh.tv_offer_hint.setVisibility(View.GONE);
                String sellTime = bean.getOfferSellRemindTime();
                vh.tv_offer_time_remind.setText(bean.getOfferSellRemindTime());
                if (bean.isLastBuyFiveFlag()) {
                    vh.gif_3.setVisibility(View.VISIBLE);
                    GifDrawable gifDrawable3 = (GifDrawable) vh.gif_3.getDrawable();
                    gifDrawable3.start();
                    vh.tv_offer_time_remind.setTextColor(colors[0]);
                } else {
                    vh.gif_2.setVisibility(View.VISIBLE);
                    GifDrawable gifDrawable2 = (GifDrawable) vh.gif_2.getDrawable();
                    gifDrawable2.start();
                    vh.tv_offer_time_remind.setTextColor(colors[1]);
                }
                if ("00:00".endsWith(sellTime)) {
                    bean.setOfferStatusCopy("嫌贵？去问价");
                    bean.setStatus(5);
                    vh.tv_offer_time_remind.setVisibility(View.GONE);

                }
                vh.vFlag.setImageResource(R.mipmap.offer_up_green);
                vh.rl_msg.setBackgroundColor(colors[4]);
                vh.tv_create_time.setTextColor(colors[1]);
                vh.tv_number_bottom.setTextColor(colors[1]);
                /*todo:倒计时 倒计时分两种 5分钟内红色*/
                break;
            case 3://绿色静图 00:00  忙碌，过会问
                vh.tv_offer_time_remind.setVisibility(View.VISIBLE);
                vh.gif_4.setVisibility(View.VISIBLE);
                vh.tv_price_old.setTextColor(colors[0]);
                vh.tv_price_old.setPaintFlags(vh.tv_price_old.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                vh.tv_offer_price.setVisibility(View.GONE);
                vh.tv_offer_hint.setText(bean.getOfferStatusCopy());
                vh.tv_offer_hint.setTextColor(colors[2]);
                vh.tv_offer_hint.setVisibility(View.VISIBLE);
                vh.tv_offer_time_remind.setText("00:00");
                vh.vFlag.setImageResource(R.mipmap.offer_up_grey);
                vh.rl_msg.setBackgroundColor(colors[5]);
                vh.tv_create_time.setTextColor(colors[2]);
                vh.tv_number_bottom.setTextColor(colors[2]);
                break;
            case 4://绿色静图 00:00 可能没有该类型
                vh.tv_offer_time_remind.setVisibility(View.VISIBLE);
                vh.gif_4.setVisibility(View.VISIBLE);
                vh.tv_price_old.setTextColor(colors[0]);
                vh.tv_price_old.setPaintFlags(vh.tv_price_old.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                vh.tv_offer_price.setVisibility(View.GONE);
                vh.tv_offer_time_remind.setText("00:00");
                vh.tv_offer_hint.setTextColor(colors[3]);

                vh.vFlag.setImageResource(R.mipmap.offer_up_grey);
                vh.rl_msg.setBackgroundColor(colors[5]);
                vh.tv_create_time.setTextColor(colors[2]);
                vh.tv_number_bottom.setTextColor(colors[2]);
                break;
            case 5://绿色静图 嫌贵？00:00 去问价
                vh.tv_offer_time_remind.setVisibility(View.GONE);
                vh.gif_4.setVisibility(View.VISIBLE);
                vh.tv_price_old.setTextColor(colors[0]);
                vh.tv_price_old.setPaintFlags(vh.tv_price_old.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                vh.tv_offer_price.setVisibility(View.GONE);
                vh.tv_offer_hint.setText(bean.getOfferStatusCopy());
                vh.tv_offer_hint.setTextColor(colors[3]);
                vh.tv_offer_hint.setVisibility(View.VISIBLE);
                vh.tv_offer_time_remind.setText("00:00");

                vh.vFlag.setImageResource(R.mipmap.offer_up_grey);
                vh.rl_msg.setBackgroundColor(colors[5]);
                vh.tv_create_time.setTextColor(colors[2]);
                vh.tv_number_bottom.setTextColor(colors[2]);
                break;
        }
    }

    public void showCartAddView(ViewHolder vh, OfferMyList bean) {
        boolean isShow = true;// = bean.isAcceptOrders()

        if (bean.getShoppingCarProductCount() == null || "".equals(bean.getShoppingCarProductCount()) || 0 == bean.getShoppingCarProductCount()) {
            isShow = false;
        } else {
            if (bean.isAcceptOrders()) {
                isShow = true;
            } else {
                isShow = false;
            }
        }

        if (isShow) {
            vh.ivCountAdd.setVisibility(View.VISIBLE);
            vh.ivCountRemove.setVisibility(View.VISIBLE);
            vh.tvCountAdd.setVisibility(View.VISIBLE);
            vh.countAddUnderlineLayout.setVisibility(View.VISIBLE);
            vh.tvChargeNumber.setVisibility(View.VISIBLE);

            vh.ivAdd.setVisibility(View.GONE);

        } else {
            vh.ivCountAdd.setVisibility(View.VISIBLE);
            vh.ivCountRemove.setVisibility(View.GONE);
            vh.tvCountAdd.setVisibility(View.GONE);
            vh.countAddUnderlineLayout.setVisibility(View.GONE);
            vh.tvChargeNumber.setVisibility(View.GONE);
            vh.ivAdd.setVisibility(View.GONE);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View ll_offer, ll_often;

        GifImageView gif_1;
        GifImageView gif_2;
        GifImageView gif_3;
        ImageView gif_4;
        TextView tv_offer_hint;
        TextView tv_offer_time_remind;

        TextView tv_create_time, tv_number_bottom;

        TextView tvTitle;
        TextView tvUnitPackage;
        TextView tvMarketName;
        ImageView iv;
        ImageView ivAdd;
        ImageView ivCountAdd;
        ImageView ivCountRemove;
        TextView tvCountAdd;
        TextView tvChargeNumber;
        TextView countAddUnderlineTv;
        LinearLayout countAddUnderlineLayout;
        TextView tvPromotionStatus;
        ImageView ivCorner;
        View vCartBg;
        View vContent;
        TextView tvMsg;
        ImageView vFlag;
        View rl_msg;
        View vArrow;


        TextView tv_price_old, tv_offer_price, tv_home_often_store, tv_activity_promotion, tv_activity_second;
        TagCloudView tag;

        View ll_home_often_no, ll_package;


        View rl_detail;
        View ll_top, v_line_top, tv_home_often_compare;
        TextView rb_1, rb_2, rb_3;


        ViewHolder(@NonNull View view) {
            super(view);
            gif_1 = view.findViewById(R.id.gif_1);
            gif_2 = view.findViewById(R.id.gif_2);
            gif_3 = view.findViewById(R.id.gif_3);
            gif_4 = view.findViewById(R.id.gif_4);
            ll_offer = view.findViewById(R.id.ll_offer);
            tv_offer_price = (TextView) view.findViewById(R.id.tv_offer_price);
            tv_offer_hint = view.findViewById(R.id.tv_offer_hint);
            tv_offer_time_remind = view.findViewById(R.id.tv_offer_time_remind);


            ll_top = view.findViewById(R.id.ll_top);
            v_line_top = view.findViewById(R.id.v_line_top);
            tv_home_often_compare = view.findViewById(R.id.tv_home_often_compare);
            rb_1 = view.findViewById(R.id.rb_1);
            rb_2 = view.findViewById(R.id.rb_2);
            rb_3 = view.findViewById(R.id.rb_3);
            ll_often = view.findViewById(R.id.ll_often);
            iv = (ImageView) view.findViewById(R.id.iv_product);
            tvTitle = (TextView) view.findViewById(R.id.tv_product_title);
            tvUnitPackage = (TextView) view.findViewById(R.id.tv_unit_package);
            tvChargeNumber = (TextView) view.findViewById(R.id.tv_charge_number);
            tv_price_old = (TextView) view.findViewById(R.id.tv_price_old);

            tv_create_time = view.findViewById(R.id.tv_create_time);
            tv_number_bottom = view.findViewById(R.id.tv_number_bottom);
            //iv_often_delete = view.findViewById(R.id.iv_often_delete);

            tv_home_often_store = (TextView) view.findViewById(R.id.tv_home_often_store);

            ivCountAdd = (ImageView) view.findViewById(R.id.iv_add_count);
            ivCountRemove = (ImageView) view.findViewById(R.id.iv_remove_count);
            vCartBg = view.findViewById(R.id.v_cart_bg);
            vContent = view.findViewById(R.id.order_ll);


            ivAdd = (ImageView) view.findViewById(R.id.iv_add);
            tvCountAdd = (TextView) view.findViewById(R.id.ed_price);
            countAddUnderlineLayout = view.findViewById(R.id.item_center_search_underline_layout);
            countAddUnderlineTv = view.findViewById(R.id.item_center_search_ed_price_underline);

            ivCorner = (ImageView) view.findViewById(R.id.iv_corner);
            tag = (TagCloudView) view.findViewById(R.id.ll_tags);


            tvMsg = (TextView) view.findViewById(R.id.tv_msg);
            vFlag = view.findViewById(R.id.iv_msg);
            rl_msg = view.findViewById(R.id.rl_msg);
            vArrow = view.findViewById(R.id.tv_msg_right);

            vCartBg = view.findViewById(R.id.v_cart_bg);

            tv_activity_promotion = view.findViewById(R.id.tv_activity_promotion);
            tv_activity_second = view.findViewById(R.id.tv_activity_second);
            tvPromotionStatus = (TextView) view.findViewById(R.id.tv_promotion_status);

            ll_home_often_no = view.findViewById(R.id.ll_home_often_no);
            ll_package = view.findViewById(R.id.ll_package);

            rl_detail = view.findViewById(R.id.rl_detail);
        }
    }
}
